<template>
	<view v-if="init" :style="{ '--font_color': fontColor || '#333','--imgwh':(imgWH || 50) * 2 + 'rpx','--title-top':(titleTop || 5) * 2 + 'rpx' }">
		<!-- 因为没有监听dataList,所以要加载好dataList在渲染该组件 -->
		<view :class="['menu_box', '_' + rowNum]" v-if="menuList.length <= 0">
			<view class="menu-box-item" :style="{ width: rowNum > 0 ? 100 / rowNum + '%' : '25%' }"
				v-for="(item, index) in dataList" :key="index" @click="goJump(item)">
				<image class="item_img" :src="item.img || item.image"></image>
				<view class="item-tit">{{ item.name || item.title }}</view>
			</view>
		</view>
		<block v-else>
			<swiper class="swiper_box" @change="changeMenu" :style="{'--item_h':itemHeight(imgWH,titleTop)}">
				<swiper-item v-for="(swipItem, swipIndex) in menuList" :key="swipIndex">
					<view :class="['menu_box', '_' + rowNum]">
						<view class="menu-box-item" :style="{ width: rowNum > 0 ? 100 / rowNum + '%' : '25%' }"
							v-for="(item, index) in swipItem" :key="index" @click="goJump(item)">
							<image class="item_img" :src="item.img || item.image"></image>
							<view class="item-tit">{{ item.name || item.title }}</view>
						</view>
					</view>
				</swiper-item>
			</swiper>
			<view class="dots_box">
				<view :class="['dots', index === currentIndex ? 'active' : '']" v-for="(item, index) in menuList"
					:key="index"></view>
			</view>
		</block>
	</view>
</template>

<script>
	import {
		pathLink,
		pageComponentsLink
	} from '@/utils/jump.js';
	export default {
		props: {
			dataList: {
				type: Array,
				default: []
			},
			rowNum: {
				type: Number,
				default: 4
			},
			fontColor: {
				type: String,
				default: '#333',
			},
			imgWH: {
				type: Number,
				default: 50,
			},
			titleTop: {
				type: Number,
				default: 5,
			},
		},
		computed: {
			itemHeight(imgWH,titleTop) {
				return (imgWH,titleTop) => {
					return (2 + 7 * 2 + imgWH + titleTop + 18) * 4 + 'rpx'
				}
			}
		},
		data() {
			return {
				init: false,
				menuList: [],
				currentIndex: 0
			};
		},
		mounted() {
			let count = (this.rowNum || 4) * 2
			if (this.dataList.length > count) {
				let menuList = [];
				for (var i = 0; i < this.dataList.length; i += count) {
					menuList.push(this.dataList.slice(i, i + count));
				}
				this.menuList = menuList;
			}
			this.init = true;
		},
		methods: {
			goJump(item) {
				if (item.mini_url) {
					pathLink({
						path: item.mini_url
					});
				} else if (item.linkUrl) {
					pageComponentsLink(item);
				}
			},
			changeMenu(e) {
				this.currentIndex = e.detail.current;
			}
		}
	};
</script>

<style scoped lang="scss">
	.swiper_box {
		height: var(--item_h);
	}

	.menu_box {
		display: flex;
		flex-wrap: wrap;
		padding: 4rpx 10rpx; // 2 计算
	}

	.menu-box-item {
		display: flex;
		flex-direction: column;
		align-items: center;
		justify-content: space-between;
		margin: 14rpx 0; // 计算

		.item_img {
			width: var(--imgwh);
			height: var(--imgwh);// 计算
		}

		.item-tit {
			color: var(--font_color);
			font-size: 26rpx;
			margin-top: var(--title-top); // 计算
			line-height: 36rpx; // 计算
			font-weight: 500;
		}
	}

	.dots_box {
		display: flex;
		align-items: center;
		justify-content: center;
		height: 50rpx;
		margin-top: -18rpx;

		.dots {
			border-radius: 50%;
			overflow: hidden;
			background-color: #ddd;
			margin: 0 6rpx;
			width: 16rpx;
			height: 16rpx;

			&.active {
				border: 2rpx solid #ddd;
				background-color: #fff;
			}
		}
	}
</style>
